package com.xxx.mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.mall.converter.AttrConverter;
import com.xxx.mall.dto.AttrDTO;
import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.exception.MallException;
import com.xxx.mall.mapper.AttrMapper;
import com.xxx.mall.model.Attr;
import com.xxx.mall.service.AttrCategoryService;
import com.xxx.mall.service.AttrService;
import com.xxx.mall.service.AttrValueService;
import com.xxx.mall.service.CategoryService;
import com.xxx.mall.vo.AttrVO;
import com.xxx.mall.vo.PageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 属性信息 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-23
 */
@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements AttrService {

    @Resource
    AttrConverter attrConverter;

    @Resource
    AttrValueService attrValueService;

    @Resource
    AttrCategoryService attrCategoryService;

    @Resource
    CategoryService categoryService;

    @Override
    public PageVO<AttrVO> pageList(PageQuery pageQuery, AttrDTO attrDTO) {
        Long total = baseMapper.countAttr(attrDTO);
        List<AttrVO> list = baseMapper.pageList((pageQuery.getPage() - 1) * pageQuery.getSize(), pageQuery.getSize(), attrDTO);
        return PageVO.of(total, list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttr(AttrDTO attrDTO) {
        //保存属性
        Attr attr = attrConverter.toAttr(attrDTO);
        baseMapper.insert(attr);
        //保存属性值
        attrValueService.saveByAttrId(attr.getAttrId(), attrDTO.getAttrValues());
        //保存属性和分类关联关系
        attrCategoryService.saveAttrCategory(attr.getAttrId(), attrDTO.getCategoryIds());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAttr(AttrDTO attrDTO) {
        Attr attr = attrConverter.toAttr(attrDTO);
        //更新属性
        baseMapper.updateById(attr);
        //更新属性值
        attrValueService.updateByAttrId(attr.getAttrId(), attrDTO.getAttrValues());
        //更新属性和分类关联关系
        attrCategoryService.updateAttrCategory(attr.getAttrId(), attrDTO.getCategoryIds());
    }

    @Override
    public AttrVO getAttrById(Long id) {
        AttrVO attrVO = baseMapper.selectByAttrId(id);
        if (attrVO == null) {
            throw new MallException("属性不存在");
        }
        attrVO.setCategories(attrCategoryService.ListByAttrId(id));
        return attrVO;
    }

    @Override
    public void delAttrById(Long id) {
        //删除属性
        //删除属性值
        //删除属性和分类关联关系
        baseMapper.removeById(id);
        //TODO spu sku中的属性值也要删除
    }

    @Override
    public List<AttrVO> listByCategoryId(Long categoryId,Integer attrType) {
        List<Long> ids = categoryService.queryAllParentId(categoryId);
        return baseMapper.queryListByCategoryIds(ids,attrType);
    }
}
